package com.ruoyi.zhgd.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.zhgd.domain.bo.NAsphaltStationDataBo;
import com.ruoyi.zhgd.domain.vo.NAsphaltStationDataVo;
import com.ruoyi.zhgd.domain.NAsphaltStationData;
import com.ruoyi.zhgd.mapper.NAsphaltStationDataMapper;
import com.ruoyi.zhgd.service.INAsphaltStationDataService;
import com.baomidou.dynamic.datasource.annotation.DS;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 沥青拌合站数据Service业务层处理
 *
 * @author zyb
 * @date 2023-09-06
 */
@RequiredArgsConstructor
@Service
@DS("sjcj")
public class NAsphaltStationDataServiceImpl implements INAsphaltStationDataService {

    private final NAsphaltStationDataMapper baseMapper;

    /**
     * 查询沥青拌合站数据
     */
    @Override
    public NAsphaltStationDataVo queryById(String id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询沥青拌合站数据列表
     */
    @Override
    public TableDataInfo<NAsphaltStationDataVo> queryPageList(NAsphaltStationDataBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<NAsphaltStationData> lqw = buildQueryWrapper(bo);
        Page<NAsphaltStationDataVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询沥青拌合站数据列表
     */
    @Override
    public List<NAsphaltStationDataVo> queryList(NAsphaltStationDataBo bo) {
        LambdaQueryWrapper<NAsphaltStationData> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<NAsphaltStationData> buildQueryWrapper(NAsphaltStationDataBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<NAsphaltStationData> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getOrganizationId()), NAsphaltStationData::getOrganizationId, bo.getOrganizationId());
        lqw.eq(StringUtils.isNotBlank(bo.getStationId()), NAsphaltStationData::getStationId, bo.getStationId());
        lqw.eq(StringUtils.isNotBlank(bo.getMatchId()), NAsphaltStationData::getMatchId, bo.getMatchId());
        lqw.eq(StringUtils.isNotBlank(bo.getGradeId()), NAsphaltStationData::getGradeId, bo.getGradeId());
        lqw.eq(bo.getDischargeTime() != null, NAsphaltStationData::getDischargeTime, bo.getDischargeTime());
        lqw.eq(bo.getDischargeTemperature() != null, NAsphaltStationData::getDischargeTemperature, bo.getDischargeTemperature());
        lqw.eq(bo.getSluiceTemperature() != null, NAsphaltStationData::getSluiceTemperature, bo.getSluiceTemperature());
        lqw.eq(bo.getBatchWeight() != null, NAsphaltStationData::getBatchWeight, bo.getBatchWeight());
        lqw.eq(bo.getMixingDuration() != null, NAsphaltStationData::getMixingDuration, bo.getMixingDuration());
        lqw.eq(bo.getAsphaltTemperature() != null, NAsphaltStationData::getAsphaltTemperature, bo.getAsphaltTemperature());
        lqw.eq(bo.getAsphaltAct() != null, NAsphaltStationData::getAsphaltAct, bo.getAsphaltAct());
        lqw.eq(bo.getAsphaltAggregateRatioAct() != null, NAsphaltStationData::getAsphaltAggregateRatioAct, bo.getAsphaltAggregateRatioAct());
        lqw.eq(bo.getAsphaltTheo() != null, NAsphaltStationData::getAsphaltTheo, bo.getAsphaltTheo());
        lqw.eq(bo.getAsphaltAggregateRatioTheo() != null, NAsphaltStationData::getAsphaltAggregateRatioTheo, bo.getAsphaltAggregateRatioTheo());
        lqw.eq(bo.getGravel1Act() != null, NAsphaltStationData::getGravel1Act, bo.getGravel1Act());
        lqw.eq(bo.getGravel1RatioAct() != null, NAsphaltStationData::getGravel1RatioAct, bo.getGravel1RatioAct());
        lqw.eq(bo.getGravel1Theo() != null, NAsphaltStationData::getGravel1Theo, bo.getGravel1Theo());
        lqw.eq(bo.getGravel1RatioTheo() != null, NAsphaltStationData::getGravel1RatioTheo, bo.getGravel1RatioTheo());
        lqw.eq(bo.getGravel2Act() != null, NAsphaltStationData::getGravel2Act, bo.getGravel2Act());
        lqw.eq(bo.getGravel2RatioAct() != null, NAsphaltStationData::getGravel2RatioAct, bo.getGravel2RatioAct());
        lqw.eq(bo.getGravel2Theo() != null, NAsphaltStationData::getGravel2Theo, bo.getGravel2Theo());
        lqw.eq(bo.getGravel2RatioTheo() != null, NAsphaltStationData::getGravel2RatioTheo, bo.getGravel2RatioTheo());
        lqw.eq(bo.getGravel3Act() != null, NAsphaltStationData::getGravel3Act, bo.getGravel3Act());
        lqw.eq(bo.getGravel3RatioAct() != null, NAsphaltStationData::getGravel3RatioAct, bo.getGravel3RatioAct());
        lqw.eq(bo.getGravel3Theo() != null, NAsphaltStationData::getGravel3Theo, bo.getGravel3Theo());
        lqw.eq(bo.getGravel3RatioTheo() != null, NAsphaltStationData::getGravel3RatioTheo, bo.getGravel3RatioTheo());
        lqw.eq(bo.getGravel4Act() != null, NAsphaltStationData::getGravel4Act, bo.getGravel4Act());
        lqw.eq(bo.getGravel4RatioAct() != null, NAsphaltStationData::getGravel4RatioAct, bo.getGravel4RatioAct());
        lqw.eq(bo.getGravel4Theo() != null, NAsphaltStationData::getGravel4Theo, bo.getGravel4Theo());
        lqw.eq(bo.getGravel4RatioTheo() != null, NAsphaltStationData::getGravel4RatioTheo, bo.getGravel4RatioTheo());
        lqw.eq(bo.getGravel5Act() != null, NAsphaltStationData::getGravel5Act, bo.getGravel5Act());
        lqw.eq(bo.getGravel5RatioAct() != null, NAsphaltStationData::getGravel5RatioAct, bo.getGravel5RatioAct());
        lqw.eq(bo.getGravel5Theo() != null, NAsphaltStationData::getGravel5Theo, bo.getGravel5Theo());
        lqw.eq(bo.getGravel5RatioTheo() != null, NAsphaltStationData::getGravel5RatioTheo, bo.getGravel5RatioTheo());
        lqw.eq(bo.getGravel6Act() != null, NAsphaltStationData::getGravel6Act, bo.getGravel6Act());
        lqw.eq(bo.getGravel6RatioAct() != null, NAsphaltStationData::getGravel6RatioAct, bo.getGravel6RatioAct());
        lqw.eq(bo.getGravel6Theo() != null, NAsphaltStationData::getGravel6Theo, bo.getGravel6Theo());
        lqw.eq(bo.getGravel6RatioTheo() != null, NAsphaltStationData::getGravel6RatioTheo, bo.getGravel6RatioTheo());
        lqw.eq(bo.getRecycledMaterialAct() != null, NAsphaltStationData::getRecycledMaterialAct, bo.getRecycledMaterialAct());
        lqw.eq(bo.getRecycledMaterialRationAct() != null, NAsphaltStationData::getRecycledMaterialRationAct, bo.getRecycledMaterialRationAct());
        lqw.eq(bo.getRecycledMaterialTheo() != null, NAsphaltStationData::getRecycledMaterialTheo, bo.getRecycledMaterialTheo());
        lqw.eq(bo.getRecycledMaterialRatioTheo() != null, NAsphaltStationData::getRecycledMaterialRatioTheo, bo.getRecycledMaterialRatioTheo());
        lqw.eq(bo.getSlagPowderAct() != null, NAsphaltStationData::getSlagPowderAct, bo.getSlagPowderAct());
        lqw.eq(bo.getSlagPowderRatioAct() != null, NAsphaltStationData::getSlagPowderRatioAct, bo.getSlagPowderRatioAct());
        lqw.eq(bo.getSlagPowderTheo() != null, NAsphaltStationData::getSlagPowderTheo, bo.getSlagPowderTheo());
        lqw.eq(bo.getSlagPowderRatioTheo() != null, NAsphaltStationData::getSlagPowderRatioTheo, bo.getSlagPowderRatioTheo());
        lqw.eq(bo.getInsertTime() != null, NAsphaltStationData::getInsertTime, bo.getInsertTime());
        return lqw;
    }

    /**
     * 新增沥青拌合站数据
     */
    @Override
    public Boolean insertByBo(NAsphaltStationDataBo bo) {
        NAsphaltStationData add = BeanUtil.toBean(bo, NAsphaltStationData.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改沥青拌合站数据
     */
    @Override
    public Boolean updateByBo(NAsphaltStationDataBo bo) {
        NAsphaltStationData update = BeanUtil.toBean(bo, NAsphaltStationData.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(NAsphaltStationData entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除沥青拌合站数据
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<String> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
